import Vue from 'vue';
import VueRouter from 'vue-router';
import i18n from "@/i18n";
import { LoadingBar } from 'view-design';
import store from '../store/index';

Vue.use(VueRouter);
const routes = [
  {
    path: '/1111-of-2024',
    name: '1111-of-2024',
    meta: {
      title: '11.11 Extravaganza｜100buy'
    },
    component: () => import('@/views/main/activityRelated/views/home/index.vue')
  },
  {
    path: '/', // home
    component: () => import('@/views/main/index.vue'),
    children: [
      {
        path: '/loginToken', //loginToken
        name: 'loginToken',
        meta: {
          title: 'routerTitle.login',
        },
        component: () => import('@/views/loginToken/index.vue')
      },
      {
        path: '', //home
        name: 'home',
        meta: {
          title: 'routerTitle.home',
        },
        component: () => import('@/views/main/home/index.vue')
      },
      {
        path: 'user', //user
        component: () => import('@/views/main/user/index.vue'),
        meta: {
          requireAuth: true,
        },
        children: [
          {
            path: 'center', //home
            name: 'center',
            meta: {
              title: 'header.userCenter',
              requireAuth: true,
            },
            component: () => import('@/views/main/user/user.vue')
          },
          {
            path: 'editProfile', //编辑个人资料
            name: 'editProfile',
            meta: {
              title: '编辑个人资料',
              requireAuth: true,
            },
            component: () => import('@/views/main/user/editProfile.vue')
          },
          {
            path: 'shopping', //购物车
            name: 'shopping',
            meta: {
              title: 'routerTitle.cart',
              requireAuth: true,
            },
            component: () => import('@/views/main/user/shopping-cart.vue')
          },
          {
            path: 'favorite', //收藏夹
            name: 'favorite',
            meta: {
              title: 'routerTitle.favorites',
              requireAuth: true,
            },
            component: () => import('@/views/main/user/favorite.vue')
          },
          {
            path: 'order', //我的订单
            name: 'order',
            meta: {
              title: 'routerTitle.myOrder',
              requireAuth: true,
            },
            component: () => import('@/views/main/user/order.vue')
          },
          {
            path: 'warehouse', //我的仓库
            name: 'warehouse',
            meta: {
              title: 'routerTitle.myWarehouse',
              requireAuth: true,
            },
            component: () => import('@/views/main/user/warehouse.vue')
          },
          {
            path: 'rehearsal', //预演包裹
            name: 'rehearsal',
            meta: {
              title: 'routerTitle.previewPackage',
              requireAuth: true,
            },
            component: () => import('@/views/main/user/rehearsal.vue')
          },
          {
            path: 'shipment', //我的包裹
            name: 'shipment',
            meta: {
              title: 'routerTitle.myPackage',
              requireAuth: true,
            },
            component: () => import('@/views/main/user/my-shipment.vue')
          },
          {
            path: 'shipment/details', //包裹详情
            name: 'shipment-details',
            meta: {
              title: 'routerTitle.packageDetails',
              requireAuth: true,
            },
            component: () => import('@/views/main/user/my-shipment/details.vue')
          },
          {
            path: 'changing/password', //修改密码
            name: 'password',
            meta: {
              title: 'routerTitle.changePassword',
              requireAuth: true,
            },
            component: () => import('@/views/main/user/account-setting/secure/password.vue')
          },
          {
            path: 'changing/email', //修改邮箱
            name: 'email',
            meta: {
              title: 'routerTitle.modifyEmail',
              requireAuth: true,
            },
            component: () => import('@/views/main/user/account-setting/secure/email.vue')
          },
          {
            path: 'login/record', //登录记录
            name: 'login-record',
            meta: {
              title: 'routerTitle.loginRecord',
              requireAuth: true,
            },
            component: () => import('@/views/main/user/account-setting/secure/login-records.vue')
          },
          {
            path: 'assets', //我的资产
            name: 'assets',
            meta: {
              title: 'routerTitle.myAssets',
              requireAuth: true,
            },
            component: () => import('@/views/main/user/assets.vue')
          },
          {
            path: 'remittanceRecord', //银行汇款记录
            name: 'remittanceRecord',
            meta: {
              title: 'routerTitle.bankRecord',
              requireAuth: true,
            },
            component: () => import('@/views/main/user/assets/remittanceRecord.vue')
          },
          {
            path: 'withdraw', //提现
            name: 'withdraw',
            meta: {
              title: 'routerTitle.withdraw',
              requireAuth: true,
            },
            component: () => import('@/views/main/user/assets/withdraw.vue')
          },
          {
            path: 'commission', //佣金提现
            name: 'commission',
            meta: {
              title: 'routerTitle.withdraw',
              requireAuth: true,
            },
            component: () => import('@/views/main/user/assets/commission.vue')
          },
          {
            path: 'withdraw/record', //提现记录
            name: 'withdraw-record',
            meta: {
              title: 'routerTitle.withdrawalRecord',
              requireAuth: true,
            },
            component: () => import('@/views/main/user/assets/withdraw-record.vue')
          },
          {
            path: 'bill', //我的账单
            name: 'bill',
            meta: {
              title: 'routerTitle.myBill',
              requireAuth: true,
            },
            component: () => import('@/views/main/user/assets/user-bill.vue')
          },
          {
            path: 'review', //我的评论
            name: 'review',
            meta: {
              title: 'routerTitle.myComment',
              requireAuth: true,
            },
            component: () => import('@/views/main/user/review.vue')
          },
          {
            path: 'consult', //我的咨询
            name: 'consult',
            meta: {
              title: 'routerTitle.myConsultation',
              requireAuth: true,
            },
            component: () => import('@/views/main/user/consult.vue')
          },
          {
            path: 'arrears', //欠款记录
            name: 'arrears',
            meta: {
              title: 'routerTitle.arrearsRecord',
              requireAuth: true,
            },
            component: () => import('@/views/main/user/assets/arrears-record.vue')
          },
          {
            path: 'points', //我的积分
            name: 'points',
            meta: {
              title: 'routerTitle.myIntegral',
              requireAuth: true,
            },
            component: () => import('@/views/main/user/points.vue')
          },
          {
            path: 'message', //站内信
            name: 'message',
            meta: {
              title: 'routerTitle.stationMessage',
              requireAuth: true,
            },
            component: () => import('@/views/main/user/message.vue')
          },
          {
            path: 'winningRecord', //中奖记录
            name: 'winningRecord',
            meta: {
              title: 'routerTitle.winning_record',
              requireAuth: true,
            },
            component: () => import('@/views/main/user/winningRecord.vue')
          },
          {
            path: 'setting', //账户设置
            name: 'setting',
            meta: {
              title: 'routerTitle.accountSetting',
              requireAuth: true,
            },
            component: () => import('@/views/main/user/account-setting/index.vue')
          },
          {
            path: 'manage', //自定义功能
            name: 'manage',
            meta: {
              title: 'routerTitle.customFunction',
              requireAuth: true,
            },
            component: () => import('@/views/main/user/manage/index.vue')
          },
          {
            path: 'invite', //推广联盟
            component: () => import('@/views/main/user/invite/index.vue'),
            children: [
              {
                path: '', //推广联盟
                name: 'invite',
                meta: {
                  title: 'routerTitle.promotionAlliance',
                  requireAuth: true,
                },
                component: () => import('@/views/main/user/invite/invite.vue'),
              },
              {
                path: 'personal-promotion', //个人推广
                name: 'invite',
                meta: {
                  title: 'personal.IndividualAffiliate',
                  requireAuth: true,
                },
                component: () => import('@/views/main/user/invite/personalPromotion.vue'),
              }, {
                path: 'team-promotion', //团队推广
                name: 'team-promotion',
                meta: {
                  title: 'personal.TeamPromotion',
                  requireAuth: true,
                },
                component: () => import('@/views/main/user/invite/teamPromotion.vue'),
              },
              {
                path: 'invite-record', //邀请记录
                name: 'invite-record',
                meta: {
                  title: 'routerTitle.invitationRecord',
                  requireAuth: true,
                },
                component: () => import('@/views/main/user/invite/invite-record.vue'),
              },
              {
                path: 'commission-record', //佣金记录
                name: 'commission-record',
                meta: {
                  title: 'user.commissionDetails',
                  requireAuth: true,
                },
                component: () => import('@/views/main/user/invite/commission-record.vue'),
              }
            ]
          },
        ]
      },
      {
        path: 'goods', //商品
        component: () => import('@/views/main/goods/index.vue'),
        children: [
          {
            path: '', //商品
            name: 'goods',
            meta: {
              title: 'routerTitle.product',
            },
            component: () => import('@/views/main/goods/goods.vue'),
          },
          {
            path: 'details', //商品
            name: 'goods-details',
            meta: {
              title: 'routerTitle.productDetails',
            },
            component: () => import('@/views/main/goods/goods-details/goods-details.vue'),
          }
        ]
      },
      {
        path: 'search', //商品
        component: () => import('@/views/main/goods/index.vue'),
        children: [
          {
            path: ':text', //商品
            name: 'search-result',
            meta: {
              title: 'routerTitle.product',
            },
            component: () => import('@/views/main/goods/goods.vue'),
          }
        ]
      },
      {
        path: 'spring', //预估运费
        name: 'spring',
        meta: {
          title: 'estimation.newYearOpen',
        },
        component: () => import('@/views/main/goods/spring.vue')
      },
      {
        path: 'products', //商品
        component: () => import('@/views/main/goods/index.vue'),
        children: [
          {
            path: 'goods-index-seo-friends', //商品
            name: '',
            meta: {
              title: 'routerTitle.product',
            },
            component: () => import('@/views/main/goods/goods.vue'),
          }
        ]
      },
      {
        path: 'product', //商品
        component: () => import('@/views/main/goods/index.vue'),
        children: [
          {
            path: ':channel/:spuNo', //商品
            name: 'goods-details-seo-friends',
            meta: {
              title: 'routerTitle.productDetails',
            },
            component: () => import('@/views/main/goods/goods-details/goods-details.vue'),
          }
        ]
      },
      {
        path: 'shop', //店铺
        meta: {
          title: 'routerTitle.shop',
        },
        component: () => import('@/views/main/shop/index.vue'),
        children: [
          {
            path: '', //店铺
            name: 'shop',
            meta: {
              title: 'routerTitle.shop',
            },
            component: () => import('@/views/main/shop/shop.vue'),
          },
          {
            path: 'details', //店铺
            name: 'shop-details',
            meta: {
              title: 'routerTitle.shopDetails',
            },
            component: () => import('@/views/main/shop/shop-details.vue'),
          },
        ]
      },
      {
        path: 'help', //帮助中心
        component: () => import('@/views/main/help/index.vue'),
        children: [
          {
            path: '', //帮助中心
            name: 'help',
            meta: {
              title: 'routerTitle.helpCenter',
            },
            component: () => import('@/views/main/help/help.vue'),
            children: [
              {
                path: 'details/:id', //常见问题详情
                name: 'help-details',
                meta: {
                  title: 'routerTitle.faqDetails',
                },
                component: () => import('@/views/main/help/help-details.vue'),
              },
            ]
          },
        ]
      },
      {
        path: 'issueView', //常见问题
        name: 'issueView',
        meta: {
          title: 'routerTitle.commonProblem',
        },
        component: () => import('@/views/main/issueView/index.vue')
      },
      {
        path: 'beginner-guide', //新手指引
        name: 'beginner-guide',
        meta: {
          title: 'routerTitle.noviceGuide',
        },
        component: () => import('@/views/main/beginner-guide/index.vue')
      },
      {
        path: 'estimation', //预估运费
        name: 'estimation',
        meta: {
          title: 'routerTitle.estimatedFreight',
        },
        component: () => import('@/views/main/estimation/index.vue')
      },
      {
        path: 'download', //app下载落地页
        name: 'download',
        meta: {
          title: 'home.down_wrapper_tit',
        },
        component: () => import('@/views/main/download/index.vue')
      },
      {
        path: 'shopWindow', //商品橱窗
        name: 'shopWindow',
        meta: {
          title: 'header.shop_tit',
        },
        component: () => import('@/views/main/shopWindow/index.vue')
      },
      {
        path: 'pay', //支付
        name: 'pay',
        meta: {
          title: 'routerTitle.payment',
          requireAuth: true,
        },
        component: () => import('@/views/main/pay/index.vue')
      },
      {
        path: 'bankRemittance', //银行汇款
        name: 'bankRemittance',
        meta: {
          title: 'pay.pay_tit_bank',
          requireAuth: true,
        },
        component: () => import('@/views/main/pay/bank-remittance.vue')
      },
      {
        path: 'result', //支付
        name: 'result',
        meta: {
          title: 'routerTitle.pay_status',
          // requireAuth: true,
        },
        component: () => import('@/views/main/pay/result.vue')
      },
      {
        path: 'categories', //所有分类
        name: 'categories',
        meta: {
          title: 'hot.hotGoods'
        },
        component: () => import('@/views/main/categories/index.vue')
      },
      {
        path: 'pay/completion', //支付成功
        name: 'pay-completion',
        meta: {
          title: 'routerTitle.paymentSuccess',
        },
        component: () => import('@/views/main/pay/completion.vue')
      },
      {
        path: 'pay/cancel', //取消支付
        name: 'pay-cancel',
        meta: {
          title: 'routerTitle.paymentCancel',
        },
        component: () => import('@/views/main/pay/cancel.vue')
      },
      {
        path: 'confirm/order', //确认订单
        name: 'confirm-order',
        meta: {
          title: 'routerTitle.confirmOrder',
          requireAuth: true,
        },
        component: () => import('@/views/main/pay/confirm-order.vue')
      },
      {
        path: 'submit/parcel', //提交包裹
        name: 'submit-parcel',
        meta: {
          title: 'routerTitle.submitParcel',
          requireAuth: true,
        },
        component: () => import('@/views/main/submit-parcel/index.vue'),
      },
      {
        path: 'confirm/package', //确认包裹
        name: 'confirm-package',
        meta: {
          title: 'routerTitle.confirmPackage',
          requireAuth: true,
        },
        component: () => import('@/views/main/submit-parcel/confirm-package.vue'),
      },
      {
        path: 'transport', //转运订单
        component: () => import('@/views/main/transport/index.vue'),
        children: [
          {
            path: '', //转运订单
            name: 'transport',
            meta: {
              title: 'routerTitle.transshipmentOrder',
              // requireAuth: true,
            },
            component: () => import('@/views/main/transport/transport.vue'),
          },
          {
            path: 'records', //转运订单记录
            name: 'transport-records',
            meta: {
              title: 'routerTitle.transhipmentOrderRecord',
              requireAuth: true,
            },
            component: () => import('@/views/main/transport/transport-records.vue'),
          },
          {
            path: 'roam', //转运状态流转操作-待支付、补差价、处理中...
            name: 'transport-roam',
            meta: {
              title: 'routerTitle.transshipmentOrder',
              requireAuth: true,
            },
            component: () => import('@/views/main/transport/transport-roam.vue'),
          },
        ]
      },
      {
        path: 'custom', //定制订购
        name: 'custom-order',
        meta: {
          title: 'routerTitle.customOrder',
        },
        component: () => import('@/views/main/custom-order/index.vue')
      },
      {
        path: 'notice', //公告
        component: () => import('@/views/main/notice/index.vue'),
        children: [
          {
            path: '', //公告列表
            name: 'notice',
            meta: {
              title: 'routerTitle.listAnnouncements',
            },
            component: () => import('@/views/main/notice/notice.vue'),
          },
          {
            path: 'details', //公告详情
            name: 'details',
            meta: {
              title: 'routerTitle.details',
            },
            component: () => import('@/views/main/notice/details.vue'),
          },
          {
            path: ':id(\\d+)', //公告详情
            name: 'details-seo',
            meta: {
              title: 'routerTitle.details',
            },
            component: () => import('@/views/main/notice/details.vue'),
          },
        ]
      },
      {
        path: '/noticeCode/:code', //公告
        name:'noticeCode',
        component: () => import('@/views/main/notice/details.vue'),
      },
      {
        path: '/get-rewards',
        name: 'get-rewards',
        meta: {
          title: 'routerTitle.activity_title'
        },
        component: () => import('@/views/main/user/activityRelated/qcActivity.vue')
      },
      {
        path: '/diy-order',
        name: 'diy-order',
        meta: {
          title: 'routerTitle.diy_order'
        },
        component: () => import('@/views/main/diy/diy-order.vue')
      },
			{
			  path: '/ev20250410',
			  name: 'ev20250410',
			  meta: {
			    title: 'Bounty for All: Share, Earn, Repeat!｜100buy'
			  },
			  component: () => import('@/views/main/activity/event/index.vue')
			},
			{
			  path: '/store',
			  name: 'store',
			  meta: {
			    title: 'Bounty for All: Share, Earn, Repeat!｜100buy'
			  },
			  component: () => import('@/views/main/goods/store.vue')
			}
    ]
  },
  {
    path: '/com-share',
    name: 'com-share',
    meta: {
      title: 'routerTitle.promotionAlliance'
    },
    component: () => import('@/views/main/activity/Promotion/view/index.vue')
  },
  {
    path: '/com-share-det',
    name: 'com-share-det',
    meta: {
      title: 'routerTitle.details'
    },
    component: () => import('@/views/main/activity/Promotion/view/detail.vue')
  },
  // {
  //   path: '/proAll',
  //   name: 'proAll',
  //   meta: {
  //     title: 'routerTitle.promotionAlliance'
  //   },
  //   component: () => import('@/views/main/proAlliance/proLayout.vue'),
  //   children: [
  //     {
  //       path: '/Alliance',
  //       name: 'Alliance-Home',
  //       meta: {
  //         title: 'routerTitle.promotionAlliance'
  //       },
  //       component: () => import('@/views/main/proAlliance/view/index.vue'),
  //     },
  //     {
  //       path: '/wallet',
  //       name: 'wallet',
  //       meta: {
  //         title: 'promote.card_tit_balance'
  //       },
  //       component: () => import('@/views/main/proAlliance/view/prowWallet.vue'),
  //     },
  //     {
  //       path: '/invite-user',
  //       name: 'invite-user',
  //       meta: {
  //         title: 'user.invitedUser'
  //       },
  //       component: () => import('@/views/main/proAlliance/view/proInvite.vue'),
  //     },
  //     {
  //       path: '/Freight-Brokerage',
  //       name: 'Freight-Brokerage',
  //       meta: {
  //         title: 'personal.ShippingFees'
  //       },
  //       component: () => import('@/views/main/proAlliance/view/freightBrokerage.vue'),
  //     },
  //     {
  //       path: '/Product-Commission',
  //       name: 'Product-Commission',
  //       meta: {
  //         title: 'routerTitle.product_title_name'
  //       },
  //       component: () => import('@/views/main/proAlliance/view/productCommission.vue'),
  //     },
  //     {
  //       path: '/Event-Bonus',
  //       name: 'Event-Bonus',
  //       meta: {
  //         title: 'promote.yel_tit_card'
  //       },
  //       component: () => import('@/views/main/proAlliance/view/eventBonus.vue'),
  //     }
  //   ]
  // },
  {
    path: '/login', //login
    name: 'login',
    meta: {
      title: 'routerTitle.login',
    },
    component: () => import('@/views/login/index.vue')
  },
  {
    path: '/register', //login
    name: 'register',
    meta: {
      title: 'routerTitle.register',
    },
    component: () => import('@/views/register/index.vue')
  },
  {
    path: '/forgot', //forgot
    name: 'forgot',
    meta: {
      title: 'routerTitle.forgot',
    },
    component: () => import('@/views/forgot/index.vue')
  },
  // 黑五
  {
    path: '/pop-up-shop',
    name: 'pop-up-shop',
    meta: {
      title: 'pop-up-shop｜100buy'
    },
    component: () => import('@/views/main/activity/blackFriday/views/home/index.vue')
  },
  // 免运费活动
  {
    path: '/free-shipping',
    name: 'free-shipping',
    meta: {
      title: 'free-shipping｜100buy'
    },
    component: () => import('@/views/main/activity/freeShipping/view/index.vue')
  }
]


const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  }
})



router.beforeEach((to, from, next) => {
  let appName = '100buy - Your Trusted China Online Shopping Solution Agent';
  window.document.title = to.meta.title ? i18n.t(to.meta.title) + ' - 100buy' : appName;
  if (to.name == 'home') {
    window.document.title = appName;
  }

  // 加载进度
  LoadingBar.start();

  // 是否需要登录
  if (to.meta.requireAuth) {
    // 验证是否存在token
    if (store.state.user.userToken) {
      next();
    } else {
      // 如果未登录，则将当前页面的路径存储到localStorage中，并跳转到登录页面
      if (to.fullPath != '/login' && to.fullPath != '/register' && to.fullPath != '/forgot') {
        localStorage.setItem('prevPage', to.fullPath);
      }
      next({
        name: 'login',
        params: {
          path: to.fullPath
        }
      })
    }
  } else if (to.meta.name == 'details') {
    console.log("此页面不受限制")
  } else {
    // 如果未登录，则将当前页面的路径存储到localStorage中，并跳转到登录页面
    if (to.fullPath != '/login' && to.fullPath != '/register' && to.fullPath != '/forgot') {
      localStorage.setItem('prevPage', to.fullPath);
    }
    next();
  }
});

router.afterEach((to) => {
  LoadingBar.finish();
});
const VueRouterPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(to) {
  return VueRouterPush.call(this, to).catch(err => err)
}

const VueRouterReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function push(to) {
  return VueRouterReplace.call(this, to).catch(err => err)
}

export default router
